package com.c.yinyuezhushou.Ui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.load.model.ModelLoader;
import com.c.yinyuezhushou.Adapter.MusicRecAdapter;
import com.c.yinyuezhushou.Dialog.LoadingDialog;
import com.c.yinyuezhushou.MRetrofit.MusicItem;
import com.c.yinyuezhushou.MRetrofit.MyObserver;
import com.c.yinyuezhushou.MRetrofit.MyRetrofit;
import com.c.yinyuezhushou.MRetrofit.RetrofitService;
import com.c.yinyuezhushou.MRetrofit.getSearch;
import com.c.yinyuezhushou.MyApplication;
import com.c.yinyuezhushou.MyTool.Immersed;
import com.c.yinyuezhushou.R;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class SearchActivity extends AppCompatActivity {

    private LinearLayout linearLayout;
    private RecyclerView recyclerView;
    private MyApplication myApplication;
    private LoadingDialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        myApplication = (MyApplication)getApplication();

        linearLayout = findViewById(R.id.search_lin);

        Immersed.setImmersed(linearLayout,this,0);//设置状态栏

        recyclerView = findViewById(R.id.search_rec);


        setEdit();

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);

        findViewById(R.id.search_image).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });



    }

    public void setEdit(){
        EditText editText = findViewById(R.id.search_edit);
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        loadingDialog = new LoadingDialog(SearchActivity.this,false,null);

        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_DONE){
                    InputMethodManager inputMethodManager = (InputMethodManager) textView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputMethodManager.hideSoftInputFromWindow(textView.getWindowToken(),0);

                    loadingDialog.show();
                    loadingDialog.setTitle("正在搜索...");
                    search(editText.getText().toString());

                    return true;
                }

                return false;
            }
        });
    }

    int i=0;

    List<MusicItem> list = new ArrayList<MusicItem>();
    MusicRecAdapter musicRecAdapter;

    public void search(String s){
        MyRetrofit.getOb(this, RetrofitService.class).Search(s,30,i).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new MyObserver<getSearch>() {
            @Override
            public void onNext(@io.reactivex.rxjava3.annotations.NonNull getSearch getSearch) {
                if (getSearch!=null){
                    if (getSearch.getCode()==200){
                        for (getSearch.ResultBean.SongsBean search:getSearch.getResult().getSongs()){
                            MusicItem musicItem = new MusicItem();
                            musicItem.setId(search.getId());
                            musicItem.setImageurl(search.getAl().getPicUrl()+"?imageView=1&thumbnail=400z400&type=webp&quality=80");
                            musicItem.setSong(search.getAl().getName());
                            musicItem.setSinger(search.getAr().get(0).getName());
                            list.add(musicItem);
                        }
                        loadingDialog.dismiss();
                        musicRecAdapter  = new MusicRecAdapter(list,myApplication.getMusicService(),getSupportFragmentManager(),null);
                        recyclerView.setAdapter(musicRecAdapter);
                        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                            @Override
                            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                                super.onScrollStateChanged(recyclerView, newState);
                                if (newState==RecyclerView.SCROLL_STATE_IDLE)
                                {
                                    addsearch(s);
                                } else{
                                    //viewPager.setUserInputEnabled(true);
                                }
                            }
                        });
                    }
                }
            }

            @Override
            public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {
                Toast.makeText(myApplication, "搜索错误", Toast.LENGTH_SHORT).show();
                if (loadingDialog!=null){
                    loadingDialog.dismiss();
                }
            }

        });

    }

    public void addsearch(String s){
        i++;
        MyRetrofit.getOb(this, RetrofitService.class).Search(s,30,i).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new MyObserver<getSearch>() {
            @Override
            public void onNext(@io.reactivex.rxjava3.annotations.NonNull getSearch getSearch) {
                if (getSearch!=null){
                    if (getSearch.getCode()==200){
                        for (getSearch.ResultBean.SongsBean search:getSearch.getResult().getSongs()){
                            MusicItem musicItem = new MusicItem();
                            musicItem.setId(search.getId());
                            musicItem.setImageurl(search.getAl().getPicUrl()+"?imageView=1&thumbnail=400z400&type=webp&quality=80");
                            musicItem.setSong(search.getAl().getName());
                            musicItem.setSinger(search.getAr().get(0).getName());
                            list.add(musicItem);
                        }
                        if (musicRecAdapter!=null){
                            musicRecAdapter.notifyDataSetChanged();
                        }

                    }
                }
            }

            @Override
            public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {
                Toast.makeText(myApplication, "搜索错误", Toast.LENGTH_SHORT).show();
                if (loadingDialog!=null){
                    loadingDialog.dismiss();
                }
            }

        });

    }
}